layui.use(['form', 'layedit', 'laydate', 'jquery'], function () {
    var form = layui.form
        , layer = layui.layer
        , layedit = layui.layedit
        , laydate = layui.laydate
        , $ = layui.$;

    // 生成uuid：
    function generateUUID() {
        let d = new Date().getTime(); // 获取当前时间
        let uuid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
            let r = (d + Math.random() * 16) % 16 | 0;
            d = Math.floor(d / 16);
            return (c == 'x' ? r : (r & 0x3 | 0x8)).toString(16);
        });
        return uuid;
    }

    // console.log(generateUUID());
    let uuid = generateUUID();

    // 生成image标签
    $('#verification_image_box').html('<img alt="图片验证码" id="verification_image">')

    // 获取图片
    function get_verification_image() {
        uuid = generateUUID();
        $('#verification_image').attr('src', '/user/image/verification/' + uuid + '/')
    }

    get_verification_image();

    //实现点击刷新
    $('#verification_image_box').click(get_verification_image);

    //三分钟自动刷新图片
    setInterval(get_verification_image, 3 * 60 * 1000);


    // 自定义验证规则
    form.verify({
      pass: [
        /^[\S]{6,12}$/
        ,'密码必须6到12位，且不能出现空格'
      ]
      ,re_pass:function (value,item){
          if (value != $('input[name="password"]').val()){
              return '两次密码不一致'
          }
        }
      ,verify:[
        /^[\S]{4}$/
        ,'验证码必须4位'
      ]
    });

    // 监听提交
    form.on('submit(login)', function(data){
      // layer.alert(JSON.stringify(data.field), {
      //   title: '最终的提交信息'
      // })
      $.post('/user/login/' + uuid + "/", data.field, function(data){
          if(data.code == 4004 || data.code == 4001 || data.code == 4002){
              layer.msg(data.msg, {icon: 5});
              return false;
          }
          if(data.code == 0){
              // 把令牌保存到localStorage
              localStorage.setItem('Authorization', data.data);
              location.href = '/static/index.html';
          }
      })
      return false;
    });

});